package JFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import Class.*;
import com.toedter.calendar.JDateChooser;
// 活期存款界面
public class DemandDepositJFrame extends JFrame implements ActionListener {
    JLabel labelMoney = new JLabel("存入金额");
    JTextField tfdMoney = new JTextField(12);
    JLabel labelCardNumber = new JLabel("银行卡号");
    JTextField tfdCardNumber = new JTextField(12);
    JLabel labelDepositeDate = new JLabel("存入日期");
    JDateChooser depositDate = new JDateChooser();
    JButton btnConfirm = new JButton("确定");
    JButton btnClear = new JButton("清空");
    public DemandDepositJFrame() {
        this.setName("村镇银行业务储蓄系统");
        this.setTitle("用户操作界面");
        this.setSize(300, 400);
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(2);
        this.setLayout(new FlowLayout());
        this.setVisible(true);

        JPanel p1 = new JPanel();
        p1.add(labelMoney);
        p1.add(tfdMoney);
        JPanel p2 = new JPanel();
        p2.add(labelCardNumber);
        p2.add(tfdCardNumber);
        JPanel p3 = new JPanel();
        p3.add(labelDepositeDate);
        p3.add(depositDate);
        JPanel p4 = new JPanel();
        p4.add(btnConfirm);
        p4.add(btnClear);
        this.add(p1);
        this.add(p2);
        this.add(p3);
        this.add(p4);
        btnConfirm.addActionListener(this);
        btnClear.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == btnConfirm) {
            try {
                addDemandDepositRecords();
                DepositSuccessJFrame depositSuccessJFrame = new DepositSuccessJFrame();
            }catch (Exception exception) {
            }
        }
        if(e.getSource() == btnClear) {
            tfdMoney.setText("");
            tfdCardNumber.setText("");
        }
    }

    public void addDemandDepositRecords() throws Exception{
        String money = tfdMoney.getText();
        String cno = tfdCardNumber.getText();
        java.util.Date date = depositDate.getDate();
        java.sql.Date sqlDate = new java.sql.Date(date.getTime());
        Statement statement = null;
        ResultSet result = null;
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String ur1 = "jdbc:mysql://localhost:3306/bank";
            String username = "root";
            String mypassword = "20222746";
            connection = DriverManager.getConnection(ur1, username, mypassword);
            statement = connection.createStatement();
            // 将定期存款记录加入到表中
            String sql = "insert into demanddeposit (money, card, depositDate) values(?,?,?)";
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setDouble(1, Double.parseDouble(money));
            pst.setString(2, cno);
            pst.setDate(3, sqlDate);
            pst.executeUpdate();
            // 修改用户的定期余额
            double m = LoginJFrame.user.getDemandMoney();
            sql = "update user set demandMoney = ? where cardNumber = ?";
            pst = connection.prepareStatement(sql);
            pst.setDouble(1, Double.parseDouble(money) + m);
            pst.setString(2, cno);
            pst.executeUpdate();
            pst.close();
            statement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
